<?php
namespace app\app\model;
use think\Model;
use think\Db;

class UsersModel extends Model{

    public $user_id;
    protected $table = "a_users";

    /**
     * 单项查询
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($id){
        return $this->where('id','eq',$id)->find();
    }

    /**
     * @param $user_id
     * @return bool
     */
    public function updateFree($user_id){
        Db::query('update a_users set free_share = free_share - 1 where id ='.$user_id);
        return true;
    }

    /**
     * 操作心数
     */
    public function reduceChance($number){
        Db::query('update a_users set chance = chance +'.$number.' where id='.$this->user_id);
        return true;
    }

    /**
     * 加积分
     * @param $point
     * @return bool
     */
    public function addPoint($point){
        Db::query('update a_users set points = points +'.$point.' where id ='.$this->user_id);
        return true;
    }

    /**
     * 加余额
     * @param $fee
     */
    public function addFee($fee){
        Db::query('update a_users set fee = fee +'.$fee .' where id='.$this->user_id);
        return true;
    }


    /**
     * 分享复活次数
     */
    public function getRevive(){
        $user = $this->field('block_share')->where('id','eq',$this->user_id)->find();
        if($user && $user['block_share']<3){
            //获取复活卡
            Db::query('update a_users set block_share = block_share + 1 where id='.$this->user_id);
        }elseif($user && $user['block_share'] >3 && $user['block_share'] <10){
            //加积分
            Db::query('update a_users set block_share = block_share + 1,points = points + 50 where id='.$this->user_id);
        }
        return true;
    }
}